package com.ala4.oxcafe.boot.repository;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * 导出任务信息落库
 *
 * @author PING
 * @date 2024/12/21 21:48
 */
@Repository
public class ExportTaskRepository {

    private final JdbcTemplate jdbcTemplate;

    public ExportTaskRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void insert(ExportTaskDTO exportTaskDTO) {
        String insertSql = """
                INSERT INTO sys_export_task (task_code, task_name, host_name, 
                service_name, tenant_id, user_id, state, 
                error_info, download_url, end_date_time, 
                create_by, create_time, update_by, update_time) 
                VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)
                """;
        Object[] args = {exportTaskDTO.getTaskCode(), exportTaskDTO.getTaskName(), exportTaskDTO.getHostName(),
                exportTaskDTO.getServiceName(), exportTaskDTO.getTenantId(), exportTaskDTO.getUserId(), exportTaskDTO.getState().toString(),
                exportTaskDTO.getErrorInfo(), exportTaskDTO.getDownloadUrl(), exportTaskDTO.getEndDateTime(),
                exportTaskDTO.getCreateBy(), exportTaskDTO.getCreateTime(), exportTaskDTO.getUpdateBy(), exportTaskDTO.getUpdateTime()};
        jdbcTemplate.update(insertSql, args);
    }

    public void update(ExportTaskDTO exportTaskDTO) {
        String updateSql = """
                update sys_export_task set error_info = ?,state = ?,download_url = ?,end_date_time = ? where task_code = ?
                """;
        Object[] args = {exportTaskDTO.getErrorInfo(), exportTaskDTO.getState().toString(), exportTaskDTO.getDownloadUrl(), exportTaskDTO.getEndDateTime(), exportTaskDTO.getTaskCode()};
        jdbcTemplate.update(updateSql, args);
    }
}
